local status_ok, which_key = pcall(require, "which-key")
if not status_ok then
  return
end

local setup = {
  plugins = {
    marks = false, -- shows a list of your marks on ' and `
    registers = true, -- shows your registers on " in NORMAL or <C-r> in INSERT mode
    spelling = {
      enabled = false, -- enabling this will show WhichKey when pressing z= to select spelling suggestions
      suggestions = 20, -- how many suggestions should be shown in the list?
    },
    -- the presets plugin, adds help for a bunch of default keybindings in Neovim
    -- No actual key bindings are created
    presets = {
      operators = false, -- adds help for operators like d, y, ... and registers them for motion / text object completion
      motions = false, -- adds help for motions
      text_objects = false, -- help for text objects triggered after entering an operator
      windows = true, -- default bindings on <c-w>
      nav = true, -- misc bindings to work with windows
      z = false, -- bindings for folds, spelling and others prefixed with z
      g = false, -- bindings for prefixed with g
    },
  },
  -- add operators that will trigger motion and text object completion
  -- to enable all native operators, set the preset / operators plugin above
  -- operators = { gc = "Comments" },
  key_labels = {
    -- override the label used to display some keys. It doesn't effect WK in any other way.
    -- For example:
    -- ["<space>"] = "SPC",
    -- ["<cr>"] = "RET",
    -- ["<tab>"] = "TAB",
  },
  icons = {
    breadcrumb = "»", -- symbol used in the command line area that shows your active key combo
    separator = "➜", -- symbol used between a key and it's label
    group = "+", -- symbol prepended to a group
  },
  popup_mappings = {
    scroll_down = "<c-d>", -- binding to scroll down inside the popup
    scroll_up = "<c-u>", -- binding to scroll up inside the popup
  },
  window = {
    border = "rounded", -- none, single, double, shadow
    position = "bottom", -- bottom, top
    margin = { 1, 0, 1, 0 }, -- extra window margin [top, right, bottom, left]
    padding = { 2, 2, 2, 2 }, -- extra window padding [top, right, bottom, left]
    winblend = 0,
  },
  layout = {
    height = { min = 4, max = 25 }, -- min and max height of the columns
    width = { min = 20, max = 50 }, -- min and max width of the columns
    spacing = 3, -- spacing between columns
    align = "left", -- align columns left, center or right
  },
  ignore_missing = true, -- enable this to hide mappings for which you didn't specify a label
  hidden = { "<silent>", "<cmd>", "<Cmd>", "<CR>", "call", "lua", "^:", "^ " }, -- hide mapping boilerplate
  show_help = true, -- show help message on the command line when the popup is visible
  triggers = "auto", -- automatically setup triggers
  -- triggers = {"<leader>"} -- or specify a list manually
  triggers_blacklist = {
    -- list of mode / prefixes that should never be hooked by WhichKey
    -- this is mostly relevant for key maps that start with a native binding
    -- most people should not need to change this
    i = { "j", "k" },
    v = { "j", "k" },
  },
}

local opts = {
  mode = "n", -- NORMAL mode
  prefix = "<Space>",
  buffer = nil, -- Global mappings. Specify a buffer number for buffer local mappings
  silent = true, -- use `silent` when creating keymaps
  noremap = true, -- use `noremap` when creating keymaps
  nowait = true, -- use `nowait` when creating keymaps
}

local mappings = {
  ['/'] = { ':CommentToggle<cr>', '注释' },

  q = { '<cmd>q<cr>', '退出' },
  a = { '<cmd>Alpha<cr>', '起始页' },

  w = {
    name = "Window",
    h = { '<C-w>h', 'left' },
    j = { '<C-w>j', 'bottom' },
    k = { '<C-w>k', 'top' },
    l = { '<C-w>l', 'right' },
  },

  f = {
    name = "文件相关",
    s = { '<cmd>w<cr>', '保存文件' },
  },

  e = { '<cmd>NvimTreeToggle<cr>', '文件管理' },

  c = { '<cmd>bdelete<cr>','关闭当前缓冲区' },

  b = {
    name = "缓冲区",
    b = { '<cmd>Telescope buffers<cr>', '缓冲区列表' },
    o = { '<cmd>%bd|e#|bd#<cr>', '关闭其它缓冲区' },
  },

  s = {
    name = "搜索相关",
    f = { '<cmd>lua require("telescope.builtin").find_files()<cr>', '搜索文件' },
    b = { '<cmd>lua require("telescope.builtin").buffers()<cr>', '搜索buffer' },
    l = { '<cmd>lua require("telescope.builtin").current_buffer_fuzzy_find()<cr>', '搜索当前' },
    o = { '<cmd>lua require("telescope.builtin").oldfiles()<cr>', '历史文件' },
    c = { '<cmd>lua require("telescope.builtin").live_grep()<cr>', '搜索代码' },
    p = { '<cmd>Telescope projects<cr>', '项目历史' },
  },

  d = {
    name = "Debug",
    d = {'<cmd>lua require("dap").toggle_breakpoint()<cr>', '开/关断点'},
    p = {'<cmd>lua require("dap").step_back()<cr>', '步回'},
    n = {'<cmd>lua require("dap").step_into()<cr>', '单步'},
    i = {'<cmd>lua require("dap").step_over()<cr>', '单步(跳过)'},
    u = {'<cmd>lua require("dap").step_out()<cr>', '步回(跳回)'},
    s = {'<cmd>lua require("dap").continue()<cr>', '启动'},
    q = {'<cmd>lua require("dap").disconnect()<cr>', '退出'},
    e = {'<cmd>lua require("dapui").eval()<cr>', '计算'},
    o = {'<cmd>lua require("dapui").toggle()<cr>', '打开面板'},
  },

  g = {
    name = "Git",
    f = { "<cmd>DiffviewFileHistory<CR>", "diff历史" },
    p = { "<cmd>DiffviewOpen<CR>", "项目diff" },
    j = { '<cmd>lua require("gitsigns").next_hunk()<cr>', '下一区块' },
    k = { '<cmd>lua require("gitsigns").prev_hunk()<cr>', '上一区块' },
    l = { '<cmd>lua require("gitsigns").blame_line()<cr>', '查看提交' },
    r = { '<cmd>lua require("gitsigns").reset_hunk()<cr>', '重置区块' },
    R = { '<cmd>lua require("gitsigns").reset_buffer()<cr>', '重置缓存' },
    s = { '<cmd>lua require("gitsigns").stage_hunk()<cr>', '暂存区块' },
    S = { '<cmd>lua require("gitsigns").stage_buffer()<cr>', '暂存缓存' },
    u = { '<cmd>lua require("gitsigns").undo_stage_hunk()<cr>', '取消暂存' },
    o = { '<cmd>Telescope git_status<cr>', '打开修改后文件' },
    b = { '<cmd>Telescope git_branches<cr>', '切换分支' },
    c = { '<cmd>Telescope git_commits<cr>', '切换提交历史' },
    d = {
      "<cmd>Gitsigns diffthis HEAD<cr>",
      "当前diff",
    },
  },

  l = {
    name = "Lsp",
    q = { '<cmd>Telescope lsp_document_symbols<cr>', '全局变量' },
    f = { '<cmd>lua vim.lsp.buf.formatting()<cr>', '格式化文件' },
    e = { '<cmd>Telescope lsp_document_diagnostics<cr>', '诊断' },
    w = { '<cmd>Telescope lsp_workspace_diagnostics<cr>', '项目诊断' },
    r = { '<cmd>lua vim.lsp.buf.rename()<cr>', '重命名' },
    d = { '<cmd>lua vim.lsp.buf.definition()<cr>', '查看代码定义' },
    h = { '<cmd>lua vim.lsp.buf.hover()<cr>', '查看注释' },
    t = { '<cmd>lua vim.lsp.buf.document_symbol()<cr>', '查看tag' },
    a = { '<cmd>lua vim.lsp.buf.code_action()<cr>', 'Actions' },
    l = { '<cmd>TroubleToggle<cr>', 'trouble' },
    c = { "<cmd>lua vim.lsp.buf.incoming_calls()<cr>", "calltree"}
  },
}

which_key.setup(setup)
which_key.register(mappings, opts)

which_key.register({
  ['/'] = { ':CommentToggle<cr>', '注释' },
}, { prefix = '<leader>', mode = 'v', noremap = true, silent = true })
